'''
$LastChangedBy: c.gillespie@ncl.ac.uk $
$Revision: 123 $
$LastChangedDate: 2009-04-24 17:27:10 +0000 (Fri, 24 Apr 2009) $

The access point to the visualisation stuff
'''

import libsbml
from VisualiseClasses import VisualiseModel, VisualiseReaction,VisualiseSpecies
from DefaultStyles import SetDefaultStyles


class VisualiseFormat(object):
    '''Static returns a single value
        web returns a piccy with an image map
    '''

    formats = ['ps', 'ps2', 'hpgl', 'pcl', 'mif', 'pic', 'gd', 'gd2', 'gif',
        'jpg', 'jpeg', 'png', 'wbmp', 'ismap', 'imap', 'cmap', 'vrml', 'vtx', 
        'mp','fig', 'svg', 'svgz', 'dia', 'dot', 'canon', 'plain', 'plain-ext',
        'xdot']

    def __init__(self, py_dot_graph):
        self.pyGraph = py_dot_graph

    def web(self, frmt='gif', prog='dot'):
        '''Returns a base64 encoded graph and an image map'''
        if frmt not in self.formats:
            raise 'Format not recognised.'
        graph = self.pyGraph.create(format=frmt, prog=prog)
        cmap = self.pyGraph.create(format='cmap', prog=prog)
        return graph, cmap

    def static(self, frmt='dot', prog='dot'):
        '''returns just one value'''
        if frmt not in self.formats:
            raise 'Format not recognised.'
        return self.pyGraph.create(format=frmt, prog=prog)
		
class VisualiseSBML(object):
    """The main visualisation class"""

    def	__init__(self, sbml_file=False, sbml_str=False, style_dict = False):
        if sbml_file:
            fp = open(sbml_file,'r')
            sbml_str = fp.read()
            fp.close()
        
        self.d = libsbml.SBMLReader().readSBMLFromString(sbml_str)
        self.m = self.d.getModel()
        sds = SetDefaultStyles(style_dict)
        self.style_dict = sds.style_dict
       
		
    def visualiseModel(self):
        '''Visualise the entire SBML model'''
        py_dot_graph = VisualiseModel(self.m, self.style_dict).getPyDot()
        return VisualiseFormat(py_dot_graph)
        
    def visualiseReaction(self, rea_num):
        '''
        Visualise a single reaction
        rea_num is the number of the reaction you want to visualise
        Numbering starts at 1
        '''
        py_dot_graph = VisualiseReaction(self.m, rea_num, self.style_dict).getPyDot()
        return VisualiseFormat(py_dot_graph)

    def visualiseSpecies(self, sp_num):
        '''
        Visualise a single species
        sp_num is the number of the species you want to visualise
        Numbering starts at 1
        '''
        py_dot_graph = VisualiseSpecies(self.m, sp_num, self.style_dict).getPyDot()
        return VisualiseFormat(py_dot_graph)





#def sbml2html(self):
#    '''Returns an html representation of the sbml'''
#		
#    filename = resource_filename(__name__, 'xmlverbatimwrapper.xsl')
#    styledoc = libxml2.parseFile(filename)
#    style = libxslt.parseStylesheetDoc(styledoc)
#    doc = libxml2.parseDoc(self.d.toSBML())
#    result = style.applyStylesheet(doc, None)
#    html_file = style.saveResultToString(result)
#    style.freeStylesheet()
#    doc.freeDoc()
#    result.freeDoc()
    
#    #Ugly hack to get rid of bad characters - Used in web-services
#    html_file = html_file.decode('ISO-8859-1')
#    html_file = html_file.replace(u'\xc2\xa0', u'&nbsp ')
#    return html_file








if __name__ == "__main__":
    v = VisualiseSBML("/tmp/BS-Spo.xml")
#    v = VisualiseSBML("/tmp/xml_files/LV2.xml")
#    print v.visualiseSpecies(1).static('dot')
#    print v.visualiseSpecies(1).static('dot')
    print v.visualiseModel().static('gif')
#    m= v.visualiseModel().web()
    
    #fp = open("graph.gif", 'w')
    #fp.write(m[0])
    #fp.close()
    
    #fp = open("graph.cmap",'w')
    #fp.write(m[1])
    #fp.close()
    
#    print 'model visualise'
#    print VisualiseSBML(sbml).visualiseModel().static('dot')

#	print 'Specie visualise'
#	print VisualiseSBML(sbml).visualiseSpecies(1).static('dot')

#	print 'Reaction visualise'
#	print VisualiseSBML(sbml).visualiseReaction(2).static('dot')
	
#	print 'Empty Model'
#	sbml1 = BasisTools()
#	sbml = sbml1.m
#	print VisualiseSBML(sbml).visualiseModel().static('dot')
	
#	print 'One Compartment 1 Species'
#	sbml1 = BasisTools()
#	sbml1.add(Compartments={'Id':'Cell'})
#	sbml1.add(Species={'Id':'sp1', 'Compartment':'Cell'})
#	sbml = sbml1.m
#	print VisualiseSBML(sbml).visualiseModel().static('dot')
	
#	sbml =  sbml1.m
#    print 'EOF'
	
